//
//  GamAIViewController.swift
//  gamlify
//
//  Created by Admin on 2024/12/8.
//

import UIKit
import SnapKit

class GamAIViewController: UIViewController {

    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "GamAI"
        label.font = .systemFont(ofSize: 34)
        return label
    }()
    
    // subTitleImageView
    private lazy var subTitleImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "gamAi_subTitle")
        return imageView
    }()
    
    // segControl
    private lazy var segmentedControl: SegmentedControl = {
        let segmentedControl = SegmentedControl(frame: .zero)
        segmentedControl.items = ["Timeline", "New Chat"]
        segmentedControl.valueChanged = { [weak self] index in
            self?.scrollToPage(index: index)
        }
        return segmentedControl
    }()
    
    // 创建一个横向滚动的scorllview
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.isScrollEnabled = true
        scrollView.isDirectionalLockEnabled = true
        scrollView.isPagingEnabled = true
        scrollView.delegate = self
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()
    
    // Add this property to track if scrolling is programmatic
    private var isProgrammaticScrolling = false
        
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let y = segmentedControl.frame.maxY + 16
        let height = AppDimensions.shared.screenHeight - y
        scrollView.frame = CGRect(x: 0, y: y, width: AppDimensions.shared.screenWidth, height: height)
        // 设置scrollview的contentview
        scrollView.contentSize = CGSize(width: AppDimensions.shared.screenWidth * 2, height: scrollView.frame.height)
    }

    // Modify the scrollToPage method
    private func scrollToPage(index: Int) {
        isProgrammaticScrolling = true
        let offset = CGPoint(x: AppDimensions.shared.screenWidth * CGFloat(index), y: 0)
        scrollView.setContentOffset(offset, animated: true)
    }
    
    private func setupUI() {
        let bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "gamAi_bg")
        view.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(45)
            make.top.equalTo(view.safeAreaLayoutGuide).offset(63)
        }
        
        view.addSubview(subTitleImageView)
        subTitleImageView.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom)
            make.height.equalTo(18)
            make.width.equalTo(193)
        }
        
        view.addSubview(segmentedControl)
        segmentedControl.snp.makeConstraints { make in
            make.top.equalTo(subTitleImageView.snp.bottom).offset(24)
            make.left.equalTo(titleLabel)
            make.width.equalTo(160)
            make.height.equalTo(30)
        }
        
        // 设置scrollview的contentview
        view.addSubview(scrollView)
        
        setupScrollView()
    }
    
    func setupScrollView() {
        let timelineVC = TimelineViewController()
        let newChatVC = NewChatViewController()
        
        addChild(timelineVC)
        addChild(newChatVC)
        
        scrollView.addSubview(timelineVC.view)
        scrollView.addSubview(newChatVC.view)
        
        let screenWidth = UIScreen.main.bounds.width
        
        timelineVC.view.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(scrollView.snp.left)
            make.width.equalTo(screenWidth)
        }
        
        newChatVC.view.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(timelineVC.view.snp.right)
            make.width.equalTo(screenWidth)
            make.height.equalToSuperview()
        }
    }
}

// Add this extension at the end of the file
extension GamAIViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if !isProgrammaticScrolling {
            let pageIndex = round(scrollView.contentOffset.x / view.frame.width)
            segmentedControl.selectedIndex = Int(pageIndex)
        }
    }

    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        isProgrammaticScrolling = false
    }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        isProgrammaticScrolling = false
    }
}
